﻿using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace HenrryTools.WinFormTools
{
    public partial class Sm4WinForm : Form
    {
        public Sm4WinForm()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 子窗体传值
        /// </summary>
        /// <param name="text"></param>
        public void AfterChildChange(string text)
        {
            var CodeList = text.Split(',');
            var CodeCount = CodeList.Count();
            cboxSm4Key.Items.AddRange(CodeList);
            cboxSm4Key.Text = CodeList[CodeCount - 1];//设置新增为当前默认值
        }
        /// <summary>
        /// 加载页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Sm4WinForm_Load(object sender, EventArgs e)
        {
            //如果有值
            if (!string.IsNullOrWhiteSpace(Properties.Settings.Default.SM4CodeKey))
            {
                var CodeList = Properties.Settings.Default.SM4CodeKey.Split(',');
                var CodeCount = CodeList.Count();
                cboxSm4Key.Items.AddRange(CodeList);
                cboxSm4Key.Text = CodeList[CodeCount - 1];//设置新增为当前默认值
            }
            //如果有值
            if (!string.IsNullOrWhiteSpace(Properties.Settings.Default.Sm4WinFormRoot) && !string.IsNullOrWhiteSpace(Properties.Settings.Default.Sm4WinFormReturn))
            {
                txtRoot.Text = Properties.Settings.Default.Sm4WinFormRoot;//保存在Settings
                txtReturn.Text = Properties.Settings.Default.Sm4WinFormReturn;//保存在Settings
            }
        }
        /// <summary>
        /// 弹窗显示新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            Sm4CodeKey sm4CodeKey = new Sm4CodeKey();
            sm4CodeKey.Dock = DockStyle.Fill;
            sm4CodeKey.AfterChangeTextDel = new Action<string>(this.AfterChildChange);//父窗体向子窗体定义委托方法
            sm4CodeKey.Show();
            sm4CodeKey.Visible = true;
        }
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(cboxSm4Key.Text.Trim()))
            {
                labMessages.Text = "SM4密钥为空！";
                labMessages.ForeColor = Color.Red;
                return;
            }
            if (string.IsNullOrEmpty(txtRoot.Text.Trim()))
            {
                labMessages.Text = "待加解密内容为空！";
                labMessages.ForeColor = Color.Red;
                return;
            }
            try
            {
                Common.SM4.MainSm4 sm4 = new Common.SM4.MainSm4();
                string Data = sm4.Encrypt_ECB(
                     cboxSm4Key.Text  //密钥
                    , false
                    , txtRoot.Text
                );
                txtReturn.Text = Data;
                DefaultLoadValue();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
           
        }
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(cboxSm4Key.Text.Trim()))
            {
                labMessages.Text = "SM4密钥为空！";
                labMessages.ForeColor = Color.Red;
                return;
            }
            if (string.IsNullOrEmpty(txtRoot.Text.Trim()))
            {
                labMessages.Text = "待加解密内容为空！";
                labMessages.ForeColor = Color.Red;
                return;
            }
            try
            {
                Common.SM4.MainSm4 sm4 = new Common.SM4.MainSm4();
                string Data = sm4.Decrypt_ECB(
                   cboxSm4Key.Text
                    , false
                    , txtRoot.Text
                );
                txtReturn.Text = Data;
                DefaultLoadValue();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }
        /// <summary>
        /// 缓存并且判断最常使用得密钥
        /// </summary>
        public void DefaultLoadValue()
        {
            var CodeNew = string.Empty;
            var CodeList = Properties.Settings.Default.SM4CodeKey.Split(',');
            foreach (var item in CodeList)
            {
                if (item != cboxSm4Key.Text) { CodeNew += item + ","; };
            }
            CodeNew += cboxSm4Key.Text;
            Properties.Settings.Default.SM4CodeKey = CodeNew;
            Properties.Settings.Default.Sm4WinFormRoot = txtRoot.Text;//保存在Settings
            Properties.Settings.Default.Sm4WinFormReturn = txtReturn.Text;//保存在Settings
            Properties.Settings.Default.Save();//保存

            Clipboard.SetDataObject(txtReturn.Text);//复制
            labMessages.Text = "加解密成功，剪贴板已复制！";
            labMessages.ForeColor = Color.Green;
        }
        /// <summary>
        /// 互换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnTranSposition_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtRoot.Text.Trim()) && string.IsNullOrEmpty(txtReturn.Text.Trim()))
            {
                labMessages.Text = "两个输入框内容为空，内容互换失败！";
                labMessages.ForeColor = Color.Red;
                return;
            }
            string txtRootValue = txtRoot.Text;
            string txtRerurnValue = txtReturn.Text;
            txtRoot.Text = txtRerurnValue;
            txtReturn.Text = txtRootValue;
        }
    }
}
